<?php

namespace frontend\controllers;

use bc\controller\FrontController;


class KnowledgeController extends FrontController
{
	
	
	/**
	 * 获取首页信息
	 */
	public function actionIndex()
	{
		
		//$callback = $this->get('callback');
                
               
		$data['error'] =  '0';
		$data['message'] = 'success';
		              
                $marqueeModel = new \bc\model\marquee\MarqueeModel();               
                $banner  = $marqueeModel->getMarqueeList(['bc_marquee.block_id' => 1, 'status' => 1]);
                $WkList  = $marqueeModel->getMarqueeList(['bc_marquee.block_id' => 2, 'status' => 1]);
                $ZbList  = $marqueeModel->getMarqueeList(['bc_marquee.block_id' => 3, 'status' => 1]);
                $JxList  = $marqueeModel->getMarqueeList(['bc_marquee.block_id' => 4, 'status' => 1]);
                	
		$data['banner'] = $banner;
                $data['wklist'] = $WkList;
                $data['zblist'] = $ZbList;
                $data['Jxlist'] = $JxList;
                
                
		
                //var_dump($data);die();

                $json_data = json_encode($data,JSON_UNESCAPED_UNICODE);
                $jsoncallback = htmlspecialchars($_REQUEST['jsoncallback']);
                echo $jsoncallback."(".$json_data.")";
                exit;
	}
	
	
	/**
	 * 直播/线下活动/视频教学详情页
	 */
	public function actionDetail()
	{
		$typeId = $this->get('id', 0);
		$page = $this->get('page', 1);
		//$banner = $this->get('banner', '');
		
                //$callback = $this->get('callback');
		$data['error'] =  '0';
		$data['message'] = 'success';
		
                $marqueeModel = new \bc\model\marquee\MarqueeModel();
                if($typeId == 1){
                     $banner  = $marqueeModel->getMarqueeList(['bc_marquee.block_id' => 6 , 'status' => 1]);
                }elseif($typeId == 2){
                     $banner  = $marqueeModel->getMarqueeList(['bc_marquee.block_id' => 7 , 'status' => 1]);
                }elseif($typeId == 3){
                     $banner  = $marqueeModel->getMarqueeList(['bc_marquee.block_id' => 8 , 'status' => 1]);
                }
               
                
                $knowledgeModel = new \bc\model\knowledge\KnowledgeModel();
                $params = array(
                         "bc_knowledge.type"  => $typeId,
                         "bc_knowledge.status" =>  1,
                            );
                $list = $knowledgeModel ->getKnowledgeList($params,$page,10);
               
                $data['banner'] = $banner;
                $data['list'] = $list;
                	
		$json_data = json_encode($data,JSON_UNESCAPED_UNICODE);
                $jsoncallback = htmlspecialchars($_REQUEST['jsoncallback']);
                echo $jsoncallback."(".$json_data.")";
                exit;
	}
        
        /*
         * 微课回顾详情页
         */
        public function  actionInfo(){
               
                $page = $this->get('page', 1);
                $cate_id = $this->get('cate_id','');
              
                $tag1_ids = $_GET['tag1_id'];
                //$tag1_id = explode(',', $tag1_ids);
                

                $tag2_ids = $_GET['tag2_id'];
                //$tag2_id = explode(',', $tag2_ids);
                
               
                $status = $this->get('status',1);
                              
                $data['error'] =  '0';
		$data['message'] = 'success';
                
                $knowledgeModel = new \bc\model\knowledge\KnowledgeModel();
                
                if($status == 1){
                    $knowledgeModel->sort='bc_knowledge.start_time desc';
                }elseif($status == 2){
                    $knowledgeModel->sort='(bc_knowledge.ys_view_count + bc_knowledge.view_count) desc';
                }
                $params  = '';
                $params  = '1 = 1';
                $params .=" And bc_knowledge.status = 1";
                $params .=" AND bc_knowledge.type = 0";
                
                if($cate_id != ''){
                         $tag1Model = new \bc\model\knowledge\KnowledgecategorytagModel();
                         $tag1List = $tag1Model ->getFTagList(['bc_knowledge_category_tag.category_id' => $cate_id , 'bc_knowledge_category_tag.category_tag_status' => 1,'bc_knowledge_tag.tag_type'=> 1,'bc_knowledge_tag.tag_status' => 1]);
                         //var_dump($tag1List);die();
                         $tag2List = $tag1Model ->getFTagList(['bc_knowledge_category_tag.category_id' => $cate_id , 'bc_knowledge_category_tag.category_tag_status' => 1,'bc_knowledge_tag.tag_type'=> 2,'bc_knowledge_tag.tag_status' => 1]);
                        // $params = ['= ','bc_knowledge.category_id', $cate_id];
                         $params .=" AND bc_knowledge.category_id = {$cate_id}";
                }else{
                    
                          /*
                           * 默认情况下获取tag2普通标签列表
                           */
                        $tagModel = new \bc\model\knowledge\TagModel();
                        $tag2List = $tagModel->getTagList(['tag_type' =>  2 ,'tag_status' => 1]);
                     }
                
                if( !empty($tag1_ids) || !empty($tag2_ids)){
                    
                     $tag1_id = explode(',', $tag1_ids);
                     $tag2_id = explode(',', $tag2_ids);
                    
                    if(empty($tag1_id)){
                        $tag1_id=array();                        
                    }elseif(empty ($tag2_id)){
                        $tag2_id=array(); 
                    }
                    
                    $tag_id = array_merge($tag1_id, $tag2_id);
                    
                    //var_dump($tag_id);die();
                    $knowledgeIds = '';
                    foreach($tag_id as $key=>$value){
                        $relationModel = new \bc\model\knowledge\RelationModel();
                        $knowledgeList = $relationModel->getList(['relation_tag_id' => $value,'relation_status' => 1]);
                        
                        $knowledgeIds = ($knowledgeIds == '')? "{$knowledgeList[0]['relation_knowledge_id']}": $knowledgeIds .",'{$knowledgeList[0]['relation_knowledge_id']}'";
                    }    
                    
                        if($knowledgeIds){
                             $knowledgeIds = "(".$knowledgeIds.")";
                             $params .=" and bc_knowledge.id in {$knowledgeIds}";
                        }else{
                             $params .=" and bc_knowledge.id in ('')";
                        }
                        
                       
                                              
                        //$params = ['in ','bc_knowledge.id', $knowledgeIds];
                       //var_dump($knowledgeIds);die();
                       
                         
                }               
                
                $list = $knowledgeModel ->getKnowledgeList($params, $page, 10);
                                                               
                $data['list'] = $list;
                $data['tag1List'] = $tag1List;
                $data['tag2List'] = $tag2List;
                                
                $json_data = json_encode($data,JSON_UNESCAPED_UNICODE);
                $jsoncallback = htmlspecialchars($_REQUEST['jsoncallback']);
                echo $jsoncallback."(".$json_data.")";
                exit;
               
                
        }
        
        public function actionViewcount(){
               
               $knowledgeId = $this->get('kid');
               $marqueeId = $this->get('mid');
               $url = $this->get('url');
               $url = urldecode($url);
               
               $knowledgeModel = new \bc\model\knowledge\KnowledgeModel();
           
            if($knowledgeId){
                             
               $knowledgeList = $knowledgeModel->getRowById($knowledgeId);
               
               $view_count = $knowledgeList['view_count'];
               $knowledgeModel->modify(['view_count' => $view_count + 1], $knowledgeId);
               
            }
            
            if($marqueeId || $url){
                
                $marqueeModel = new \bc\model\marquee\MarqueeModel();
                $marqueeList = $marqueeModel->getRowById($marqueeId);
                $mview_count = $marqueeList['view_count'];
                $marqueeModel->modify(['view_count' => $mview_count + 1 ], $marqueeId); 
                
                //$knowledgeList = $knowledgeModel->
                $params = array(
                    'link' => $url,
                );
                $knowledgeList = $knowledgeModel->getRow($params);
                //var_dump($knowledgeList);die();
                $view_count = $knowledgeList['view_count'];
               
                $sql = "UPDATE bc_knowledge SET view_count = '$view_count'+1  WHERE link = '$url'";
                $knowledgeModel->getDb()->createCommand($sql)->execute();
                
            }
            
            $data['error'] =  '0';
            $data['message'] = 'success';
               
            $json_data = json_encode($data,JSON_UNESCAPED_UNICODE);
                $jsoncallback = htmlspecialchars($_REQUEST['jsoncallback']);
                echo $jsoncallback."(".$json_data.")";
                exit;   

               
        }

}

